package org.netty.demo.lihh.notice.strategy.factory;

import org.netty.demo.lihh.notice.domain.ClientMsgProtocol;
import org.netty.demo.lihh.notice.strategy.StrategyHandle;

import java.util.HashMap;
import java.util.Map;

public final class StrategyFactory {
  private static final Map<Integer, StrategyHandle> strategyCache = new HashMap<>();
  
  /**
   * 注册 策略
   *
   * @param state          策略状态
   * @param strategyHandle 策略事件
   * @author lihh
   */
  public static void register(Integer state, StrategyHandle strategyHandle) {
    strategyCache.put(state, strategyHandle);
  }
  
  /**
   * 执行策略
   *
   * @param state 策略状态
   * @param o     参数
   * @author lihh
   */
  public static void execute(Integer state, Object o) {
    StrategyHandle strategyHandle = strategyCache.get(state);
    if (null == strategyHandle) return;
    
    strategyHandle.accept((ClientMsgProtocol) o);
  }
}
